# Makefile for loader&kernel

# Entry point of Orange'S
# It must have the same value with 'KernelEntryPointPhyAddr' in load.inc!
ENTRYPOINT  = 0x30400

# Offset of entry point in kernel file
# It depends on ENTRYPOINT
ENTRYOFFSET =   0x400

# Programs, flags, etc.
ASM     = nasm
CC          = gcc
LD          = ld
ASMFLAGS    = -f elf
CFLAGS      = -c
LDFLAGS = -s -Ttext $(ENTRYPOINT)

# This Program
LOADER  = loader.bin
KERNEL      = kernel.bin
OBJS        = kernel.o start.o kliba.o string.o

# All Phony Targets
.PHONY : everything final image clean realclean all buildimg

# Default starting position
everything : $(LOADER) $(KERNEL)

all : realclean everything

final : all clean

image : final buildimg

clean :
	rm -f $(OBJS)

realclean :
	rm -f $(OBJS) $(LOADER) $(KERNEL)

# We assume that "a.img" exists in current folder
buildimg :
	sudo mount -o loop a.img a
	sudo cp -fv loader.bin a
	sudo cp -fv kernel.bin a
	sudo umount a

loader.bin : ./loader/loader.asm ./loader/include/load.inc ./loader/include/fat12hdr.inc ./loader/include/pm.inc
	$(ASM) $< -o $@

$(KERNEL) : $(OBJS)
	$(LD) $(LDFLAGS) $(OBJS) -o $(KERNEL)

kernel.o : ./kernel/kernel.asm
	$(ASM) $(ASMFLAGS) $< -o $@

start.o : ./kernel/start.c ./include/type.h ./include/const.h ./include/protect.h
	$(CC) $(CFLAGS) $< -o $@

kliba.o : ./lib/kliba.asm
	$(ASM) $(ASMFLAGS) $< -o $@

string.o : ./lib/string.asm
	$(ASM) $(ASMFLAGS) $< -o $@

